<template>
  <div class="history-wrap">
    <div class="history_info">
      <h2>{{ title }}</h2>
      <div class="btn"></div>
    </div>
    <div class="history-contain">
      <div class="l-line"></div>
      <ul class="history-list">
        <li
          :class="{ new_date: isNewDate(item.startTime) }"
          v-for="item in historyData"
          :key="item.value.id"
        >
          <div class="time">
            <div class="date-time">
              <i></i>
              <span>{{ formatTime(item.startTime, 'date') }}</span>
            </div>
            <div class="specific-time">
              <i class="triangle"></i>
              <span>{{ formatTime(item.startTime) }}</span>
            </div>
          </div>
          <div class="r-info">
            <HistoryBlock :data="item.value" />
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'

import HistoryBlock from '../../../components/Block/HistoryBlock.vue'

import { formatDate } from '../../../hook/utils'
import { Storage_ } from '@/class/Storage'

export default {
  name: 'UserHistory',
  components: {
    HistoryBlock
  },
  props: {
    title: {
      default: ''
    }
  },
  setup() {
    // 历史数据
    const historyData = ref()
    let dateArr = []

    historyData.value = Storage_.getItem('anime')

    // 判断是否为新日期
    function isNewDate(startTime) {
      const dateStr = formatDate(startTime, 'yyyy-mm-dd')

      const isV = dateArr.some((item) => {
        return item === dateStr
      })
      console.log(isV)

      if (!isV) {
        dateArr.unshift(dateStr)
      }

      return !isV
    }

    let formatTime = computed(() => (startTime, type = 'time') => {
      return type === 'time' ? formatDate(startTime, 'hh:ii') : formatDate(startTime, 'yyyy-mm-dd')
    })

    return {
      historyData,
      formatTime,
      isNewDate
    }
  }
}
</script>

<style lang="scss" scoped>
$margin: 80px;
$t-margin: 10px;
.history-wrap {
  padding: 30px 20px 0;
  box-sizing: border-box;
  .history_info {
    margin-bottom: 17px;
    color: $font;
    h2 {
      font-weight: 300;
    }
  }
}
.history-contain {
  display: flex;
  padding: 0 30px;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  .l-line {
    margin-right: $margin;
    box-sizing: border-box;
    border-left: 1px solid $font_unactive;
  }
}
.history-list {
  flex: 1;
  width: 100%;
  height: 100%;
  li {
    position: relative;
    padding: 10px 0;
    .time {
      font-size: 14px;
      .specific-time {
        position: absolute;
        top: 50%;
        left: -$margin;
        transform: translateY(-50%);
        display: flex;
        color: $font_unactive;
        align-items: center;
        .triangle {
          margin-right: 5px;
          width: 0;
          height: 0;
          color: transparent;
          border: 5px solid transparent;
          border-left: 5px solid $font_unactive;
          // background: linear-gradient(
          //   45deg,
          //   $font_unactive,
          //   $font_unactive 50%,
          //   transparent 50%,
          //   transparent 100%
          // );
        }
      }
      .date-time {
        display: none;
      }
    }
    .r-info {
      height: 100px;
    }
  }
  .new_date {
    margin-top: $t-margin;
    .date-time {
      position: absolute;
      top: -$t-margin;
      left: -$margin;
      // display: block !important;
      display: flex !important;
      z-index: 2;
      align-items: center;
      transform: translateY(-50%);
      i {
        margin-right: 4px;
        width: 10px;
        height: 10px;
        border-radius: 50px;
        background-color: rgb(190, 190, 190);
        transform: translateX(-50%);
      }
      span {
        flex: 1;
        font-size: 14px;
        color: $font_unactive;
      }
    }
  }
}
</style>
